package com.nit.controller;
import com.alibaba.fastjson.JSONObject;
import com.nit.dto.MemberDTO;
import com.nit.dto.utils.OpResultDTO;
import com.nit.service.MemberService;
import com.nit.utils.HttpRequestReader;
import com.nit.utils.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = "/api/member")
public class MemberController {
    @Resource
    private MemberService memberService;
    private final Logger logger = LoggerFactory.getLogger(MemberController.class);

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public List<MemberDTO> query(@RequestBody MemberDTO memberDTO) {
        List<MemberDTO> memberDTOS = new ArrayList<>();
        try {
            memberDTOS = memberService.query(memberDTO.getMemberName());
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return memberDTOS;
    }

    /**
    * @Description:
    * @Author: Lc
    * @Date: 2019/9/9
    */

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResultDTO = new OpResultDTO(-1L, SystemConstant.OP_FAILURE_OBJ);
        try {
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            opResultDTO.setLongResult(memberService.remove(jsonObject.getString("memberIds")));
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (opResultDTO.getLongResult() != -1L) {
            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
        }
        return opResultDTO;
    }

    @RequestMapping(value = "/change", method = RequestMethod.POST)
    public OpResultDTO change(HttpServletRequest request) {
        OpResultDTO opResultDTO = new OpResultDTO(-1L, SystemConstant.OP_FAILURE_OBJ);
        try {
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            opResultDTO.setLongResult(memberService.change(jsonObject.getString("memberIds")));
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (opResultDTO.getLongResult() != -1L) {
            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
        }
        return opResultDTO;
    }

//    @RequestMapping(value = "/add", method = RequestMethod.POST)
////    public OpResultDTO add(@RequestBody MemberDTO memberDTO) {
////        OpResultDTO opResultDTO = new OpResultDTO(-1L, SystemConstant.OP_FAILURE_OBJ);
////        try {
////            opResultDTO.setLongResult(memberService.add(memberDTO));
////        } catch (Exception e) {
////            logger.error(e.toString());
////        }
////        if (opResultDTO.getLongResult() != -1L) {
////            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
////        }
////        return opResultDTO;
////    }
}
